<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="12" :offset="6">
                <div class="grid-content bg-purple">
                    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                             class="demo-ruleForm" @keyup.enter.native="submitForm('ruleForm')">

                        <el-form-item label="用户名" prop="username">
                            <el-input v-model.number="ruleForm.username"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="验证码" prop="password">
                            <el-input type="text" v-model="ruleForm.code" autocomplete="off"></el-input>
                            <button @click="changeCode">{{newCode}}</button>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm') ">提交</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-col>
        </el-row>

    </div>
</template>

<script>

    export default {
        name: "Login",
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {//判断值和类型相等
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validateName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入用户名'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    username: '',
                    password: '',
                    code:'',
                },
                rules: {
                    password: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    username: [
                        {validator: validateName, trigger: 'blur'}
                    ]
                },
                newCode:'',
            };
        },
        created() {
            this.changeCode();
        },

        methods: {
            changeCode() {
                //do...while(条件) 循环
                do
                    this.newCode = Math.floor(Math.random() * 10000);
                while (this.newCode < 1000)


            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //验证验证码的正确性进行提交,ruleForm.code=用户输入的验证码
                        if(this.ruleForm.code==this.newCode){
                            this.axios.get("http://localhost:8081/user/login", {params: this.ruleForm}).then(res => {
                                console.log(res);
                                if (res.data == false) {
                                    this.$message("登陆失败")
                                } else {
                                    //存到浏览器端    localStorage本地存储存到浏览器
                                    localStorage.setItem("Authorization", res.data.data)
                                    //this.$store.dispatch("loginAction",res.data.data)//这一步写在跳转之前
                                    //登录成功跳转到首页
                                    //this.$store.commit("changeLogin",res.data)//这一步写在跳转之前
                                    this.$message("登陆成功");
                                    this.$router.push({path: "/home"})
                                }

                            })
                        }else{
                            this.$message.error("验证码错误");
                            return false;
                        }

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        computed:{
            cansubmit(){
                const {username,password} = this.user
                return Boolean(username && password)
            }
        }
    }
</script>

<style scoped>

</style>